
package com.mephi.controller.action;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import com.mephi.MephistoChess;
import com.mephi.exception.InvalidGameFormatException;
import com.mephi.gui.GUI;
import com.mephi.interfaces.ImportExportGame;
import com.mephi.interfaces.LoginManager;
import com.mephi.interfaces.Profile;
import com.mephi.io.ImportExportGameImpl;
import com.mephi.model.Move;
import com.mephi.model.profile.LoginManagerImpl;
import com.mephi.model.profile.ProfileMapSingleton;
import com.mephi.util.Ext;
import com.mephi.util.filter.FileFilter;


/**
 * Simple implementation of import game action in another thread.
 * @author mephisto
 * 2011-05-28
 * 1.0
 */
public class ImportGameAction implements Runnable
{
	public ImportGameAction(Profile profile)
	{
		this.profile = profile;
	}
	
	@Override
	public void run()
	{
		JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        List<String> extensionList =  new ArrayList<String>();
        extensionList.add(Ext.txt.toString());
        chooser.setFileFilter(new FileFilter(extensionList));
        int r = chooser.showOpenDialog(MephistoChess.gui.getFraMainFrame());
        if (r != JFileChooser.APPROVE_OPTION) return;
        logger.info("Selected file: " + chooser.getSelectedFile().getAbsolutePath());

        String filename =  chooser.getSelectedFile().getAbsolutePath();
        
		ImportExportGame<Move> importer = new ImportExportGameImpl();
		try
		{
			logger.info("Import has been started");
			LoginManager loginManager = LoginManagerImpl.INSTANCE;
			if (loginManager.isSomebodyLoggedIn())
			{
				profile.getGameMap().put("1", importer.importGame(filename));
				ProfileMapSingleton.INSTANCE.getProfiles().put(profile.getName(), profile);
				//repainting chess game score
				GUI.INSTANCE.repaintMovesPanel();
				logger.info("Import has been finished");
			}
			else
			{
				String errMsg = "Nobody is currently logged in! Please log in first!";
				logger.warn(errMsg);
				JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(),
						errMsg, title, JOptionPane.ERROR_MESSAGE);
			}
			
		}
		catch (FileNotFoundException e)
		{
			StringBuilder sB = new StringBuilder("File in the path ");
			sB.append(filename).append("doesn't exist!");
			logger.error(sB);
			logger.error(e);
			JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(), 
					sB, title, JOptionPane.ERROR_MESSAGE);
		}
		catch (IOException e)
		{
			logger.error(e);
			JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(), 
					e, title, JOptionPane.ERROR_MESSAGE);
		}
		catch (InvalidGameFormatException e)
		{
			logger.error(e);
			JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(), 
					e, title, JOptionPane.ERROR_MESSAGE);
		}
	}
	
	
	private Logger logger = Logger.getLogger("LOG");
	private Profile profile;
	private static String title = "Import error";
}
